package com.jdojo.control;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<String> tableView = new TableView<>();

        // 添加表格列
        TableColumn<String, String> column = new TableColumn<>("Actions");
        column.setCellValueFactory(data -> new SimpleStringProperty(data.getValue()));

        // 设置单元格工厂
        column.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() {
            @Override
            public TableCell<String, String> call(TableColumn<String, String> param) {
                return new TableCell<String, String>() {
                    // final Button button = new Button("Action");
                    final TextField tf = new TextField();

                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(tf);
                        }
                    }
                };
            }
        });

        // 添加数据
        tableView.getItems().addAll("Row 1", "Row 2", "Row 3");

        // 添加表格列
        tableView.getColumns().add(column);

        primaryStage.setScene(new Scene(tableView, 300, 200));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
